define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $('.btn-add').data('area', ['100%', '100%']);
            $('.btn-edit').data('area', ['100%', '100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/activity/index' + location.search,
                    add_url: 'activity/activity/add',
                    edit_url: 'activity/activity/edit',
                    del_url: 'activity/activity/del',
                    multi_url: 'activity/activity/multi',
                    import_url: 'activity/activity/import',
                    table: 'activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'cate_name',
                            title: __('关联分类'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var cateNameArray = value.split(',');
                                    for (var i = 0; i < cateNameArray.length; i++) {
                                        array.push('<span class="label bg-purple">' + cateNameArray[i] + '</span>');
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },
                        {
                            field: 'image',
                            title: __('Image'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            events: Table.api.events.images,
                            formatter: Table.api.formatter.images
                        },
                        {
                            field: 'video_image',
                            title: __('视频封面'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            events: Table.api.events.images,
                            formatter: Table.api.formatter.images
                        },
                        {
                            field: 'video', title: __('Video'), operate: 'LIKE', table: table, class: 'autocontent',
                            formatter: function (row, value) {
                                return '<video controls width="250" height="100"><source src="' + value.video + '" type="video/webm"></video>';
                            }
                        },
                        /*{
                            field: 'group_name',
                            title: __('Group_id'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var groupNameArray = value.split(',');
                                    for (var i = 0; i < groupNameArray.length; i++) {
                                        array.push('<span class="label bg-purple">' + groupNameArray[i] + '</span>');
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },*/
                        /*{
                            field: 'punchUserNickname',
                            title: '用户昵称',
                            operate: 'LIKE',
                            table: table,
                            align: 'left',
                            class: 'autocontent',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var nicknameArray = value.split('|');
                                    for (var i = 0; i < nicknameArray.length; i++) {
                                        array.push('<span class="label bg-blue">' + nicknameArray[i] + '</span>');
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },*/
                        {
                            field: 'toDayPunchNumber',
                            title: '打卡数据',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent'
                        },
                        /*{
                            field: 'goods_name',
                            title: __('GoodsName'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var nicknameArray = value.split(',');
                                    for (var i = 0; i < nicknameArray.length; i++) {
                                        array.push('<span class="label bg-blue">' + nicknameArray[i] + '</span>');
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },*/
                        /*{
                            field: 'type', title: __('Type'),
                            searchList: {1: __('Type One'), 2: __('Type Two'), 3: __('Type Three'), 4: __('Type Four')},
                            operate: false,
                            formatter: Table.api.formatter.label
                        },*/
                        /*{field: 'method', title: __('Method'),
                            searchList:{1:__('Method One'), 2:__('Method Two')},
                            formatter: Table.api.formatter.label
                        },*/
                        {
                            field: 'signup_start_date',
                            title: __('报名开始时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'signup_end_date',
                            title: __('报名结束时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'startdate',
                            title: __('活动开始时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'endtime',
                            title: __('活动结束时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'sign_start_date',
                            title: __('打卡开始时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'sign_end_date',
                            title: __('打卡结束时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {field: 'punchTotalNumber', title: __('CountPunchNumber')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '查看报名用户',
                                    title: '查看报名用户',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    url: 'activity/activity/detail',
                                }
                            ]
                        }
                    ]
                ],
                // 导出图片过大处理
                exportOptions: {
                    ignoreColumn: [0, 'operate'],
                    onBeforeSaveToFile: function (data, fileName, type, charset, encoding, bom) {
                        return $.fn.bootstrapTable.defaults.extend.savestatus;
                    },
                    onCellHtmlHyperlink: function ($cell, rowIndex, colIndex, href, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;
                        var result = '';

                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            } else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            } else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "40").attr("height", "40").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                } else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    }
                }
            });

            table.on('post-body.bs.table', function () {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: "activity/activity/detail?ids=" + Fast.api.query('ids'),
                    table: 'application',
                }
            });
            var table = $("#tables");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search: false,
                showToggle: false,
                commonSearch: false,
                showExport: false,
                showColumns: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {
                            field: 'user.avatar', title: __('photo'), operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {field: 'user.nickname', title: __('nickname'), operate: 'LIKE'},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                var typeChecked = parseInt($("input[name='row[type]']:checked").val());
                if (typeChecked === 1) {
                    $('.c-goods_ids').css('display', 'none');
                }

                $("input[name='row[type]']").click(function () {
                    if ($(this).val() == 1) {
                        $('.c-goods_ids').css('display', 'none');
                    } else {
                        $('.c-goods_ids').css('display', 'block');
                    }
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
